﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;

public partial class User_Login : System.Web.UI.Page
{
    DataClassesDataContext db = new DataClassesDataContext();

    protected void Page_Load(object sender, EventArgs e)
    {
        if(Session["Name"]!=null)
        {
            string Identity = Session["Identity"].ToString();
            if (Identity == "Developer")
            {
                Response.Redirect("~/UserLogin/Developer/DeveloperIndex.aspx");
            }
            else if (Identity == "ProjectManager")
            {
                Response.Redirect("~/UserLogin/ProjectManager/ProjectManagerIndex.aspx");
            }
            else if (Identity == "Leader")
            {
                Response.Redirect("~/UserLogin/Leader/LeaderIndex.aspx");
            }
        }


        if (Request.QueryString["JobId"]!=null)
        {

            if(Request.QueryString["Type"]== "Register")
                ClientScript.RegisterStartupScript(this.GetType(), "successMessage", "<script> alert('新用户注册成功！');</script>");
            else if(Request.QueryString["Type"] =="FindBackPassword")
                ClientScript.RegisterStartupScript(this.GetType(), "successMessage", "<script> alert('密码找回成功，请查看用户邮箱！');</script>");


            TextBox1.Text = Request.QueryString["JobId"];
        }

    }




    protected void Button1_Click(object sender, EventArgs e)
    {


        if(Page.IsValid)
        {
            string JobId = TextBox1.Text.Trim();
            string Password = TextBox2.Text.Trim();

            var result = (from r in db.UserInfo
                         where r.JobId == JobId && r.Password == Password
                         select r).FirstOrDefault();

            if (result == null)
            {
                ClientScript.RegisterStartupScript(this.GetType(), "loginFailMessage", "<script> alert('工号或密码有误，请重新登陆！');</script>");
            }
            else
            {
                string Name = ((UserInfo)result).Name;
                string Identity = ((UserInfo)result).Identity;

                Session["Name"] = Name;
                Session["Identity"] = Identity;
                if (Identity == "Developer")
                {
                    Response.Redirect("~/UserLogin/Developer/DeveloperIndex.aspx");
                }else if (Identity == "ProjectManager")
                {
                    Response.Redirect("~/UserLogin/ProjectManager/ProjectManagerIndex.aspx");
                }else if (Identity == "Leader")
                {
                    Response.Redirect("~/UserLogin/Leader/LeaderIndex.aspx");
                }

            }

        }
    }


}